package com.nbjtjc.safe.job.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.job.utils.ScheduleUtils;
import com.nbjtjc.safe.mapper.emg.EmgPlanPublicMapper;
import com.nbjtjc.safe.mapper.emg.EmgWorkBriefMapper;
import com.nbjtjc.safe.mapper.sys.SysUserMapper;
import com.nbjtjc.safe.model.emg.EmgPlanPublic;
import com.nbjtjc.safe.model.emg.EmgWorkBrief;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.util.DateUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @auther guqianbin
 * 应急响应简报全部定时发送，根据应急响应的上报时间间隔
 * 进行定时发送没有问题的简报，如果这个时间段范围内有发送简报，那就不发送定时简报
 * @date 2018/9/14 16:18
 **/
@Component("workBriefAllScheduleTask")
public class SendWorkBriefAllScheduleTask {

    @Autowired
    private EmgPlanPublicMapper emgPlanPublicMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private EmgWorkBriefMapper emgWorkBriefMapper;


    @Autowired
    private Scheduler scheduler;

    private final String SPLIT_STR = "-";

    /**
     * @param paramIds planPublicId-CompanyId-UserId
     */
    public void sendOkWorkBrief(String paramIds,Integer jobId) {
        List<String> ids = new ArrayList<>();
        ids.addAll(Arrays.asList(paramIds.split(SPLIT_STR)));
        Integer startPlanPublicId = Integer.parseInt(ids.get(0));
        Integer companyId=Integer.parseInt(ids.get(1));
        Integer userId = Integer.parseInt(ids.get(2));
        //TODO status 暂时写死
//        EmgPlanPublic emgPlanPublic = emgPlanPublicMapper.getRunningEmgPlanPublicByStatus(startPlanPublicId, 3);
//        if (ObjectUtil.isNotNull(emgPlanPublic)) {
//            publicWorkBrief(emgPlanPublic,userId);
//        }else{
//            //如果没有处于响应状态的应急响应，则删除定时发送默认工作简报的任务
//            ScheduleUtils.deleteScheduleJob(scheduler, new Integer(jobId).longValue());
//        }
    }



    private void publicWorkBrief(EmgPlanPublic emgPlanPublic,int userId){
        Date now = new Date();

        Date publicTime = emgPlanPublic.getSignDate() == null
                ? emgPlanPublic.getCreateDate() : emgPlanPublic.getSignDate();
        List<Date> sendTimeRanges= DateUtils.getEmgSendDate(emgPlanPublic.getTimeInterval(),publicTime);
        Date startDate=sendTimeRanges.get(0);
        Date endDate=sendTimeRanges.get(1);

        SysUser sysUser = sysUserMapper.selectByUserId(userId);
        int companyId = sysUser.getCompanyId();
        EmgWorkBrief emgWorkBrief = new EmgWorkBrief();
        emgWorkBrief.setCompanyId(companyId);
        emgWorkBrief.setReportUserId(userId);
        Page page = new Page();
        page.setStartDate(startDate);
        page.setEndDate(endDate);
        List<EmgWorkBrief> emgWorkBriefs = emgWorkBriefMapper.search(emgWorkBrief, page);
        if (CollectionUtil.isEmpty(emgWorkBriefs)) {
            EmgWorkBrief workBrief = new EmgWorkBrief();
            workBrief.setCompanyId(companyId);
            workBrief.setReportUserId(userId);
            workBrief.setReportTime(now);
            workBrief.setDefenseWork("已部署到位");
            workBrief.setDutyPeople("值班人员已到位");
            workBrief.setDisasterSituation("无");
            workBrief.setEmergency("");
            workBrief.setEmergencyAfter("");
            workBrief.setEmergencyHandle("");
            workBrief.setStatus(1);
            workBrief.setDeptId(sysUser.getDeptId().intValue());
            workBrief.setPlanPublicId(emgPlanPublic.getStartPlanId());
            emgWorkBriefMapper.insertSelective(workBrief);
        }
    }
}
